package pkg

import (
	"fmt"
	"log"

	"golang.org/x/crypto/ssh"
)

func SSHRun(hosts string, command string) {
	// ssh 配置
	config := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.Password("1"),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}

	// 连接服务器
	client, err := ssh.Dial("tcp", hosts+":22", config)
	if err != nil {
		log.Fatal("连接失败: ", err)
	}
	defer client.Close()

	// 创建会话
	session, err := client.NewSession()
	if err != nil {
		log.Fatal("创建会话失败: ", err)
	}
	defer session.Close()

	// 执行命令
	output, err := session.CombinedOutput(command)
	if err != nil {
		log.Fatal("执行命令失败: ", err)
	}
	fmt.Printf("输出结果: \n%s", string(output))
}
